package com.zswl.decorate.ui;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zswl.decorate.R;
import com.zswl.decorate.base.BackActivity;
import com.zswl.decorate.util.LogUtil;

import butterknife.BindView;
import io.flutter.facade.Flutter;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterView;

public class FlutterDemoActivity extends BackActivity {
    @BindView(R.id.ll_container)
    LinearLayout linearLayout;

    public static void startMe(Context context) {
        Intent intent = new Intent(context, FlutterDemoActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_flutter_demo;
    }

    @Override
    protected void init() {
        FlutterView flutterView = Flutter.createView(this, getLifecycle(), "route1");
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, -1);
        linearLayout.addView(flutterView, layoutParams);

        final FlutterView.FirstFrameListener[] listeners = new FlutterView.FirstFrameListener[1];
        listeners[0] = new FlutterView.FirstFrameListener() {
            @Override
            public void onFirstFrame() {
                linearLayout.setVisibility(View.VISIBLE);
            }
        };
        flutterView.addFirstFrameListener(listeners[0]);

        // 自定义插件
        String CHANNEL = "demo.plugin";
        new MethodChannel(flutterView, CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call.method.equals("interaction")) {
                    String msg = call.argument("1");
                    LogUtil.d("Flutter Msg:" + msg);
                    Intent intent = new Intent(FlutterDemoActivity.this, FlutterListViewActivity.class);
                    FlutterDemoActivity.this.startActivity(intent);
                    result.success("success");
                } else {
                    result.notImplemented();
                }
            }
        });

    }


}
